﻿#region File Description 
///////////////////////////////////////////////////////////////////////////////
/// \file Sprite.cs
/// \brief This file stores the Sprite Class
/// \note It does simple Sprite Handling (Loading, Rendering, Bounds Checking)
/// \author Steven Duda 
/// \date 14/5/2010
/// \remarks Copyright(c) 2010 LM01 Project Group (Animus Games - ViralFlux)
/// \remarks Some Rights Reserved.
/// Licenced under: Microsoft Reciprocal License (Ms-RL)
/// For More Information see: www.microsoft.com/opensource/licenses.mspx
///////////////////////////////////////////////////////////////////////////////
#endregion

#region Using Statements 
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using System;
#endregion 

namespace GameOfLife
{
    /// <summary>
    /// \class Sprite
    /// \author Steven Duda
    /// \brief Sprite is the base class of any renderable sprite object
    /// Is Implemented as an IDisposable, so unmanaged resources can be handled
    /// </summary>
	internal abstract class Sprite : IDisposable
    {
        #region Fields and Properties

        private bool enabled;
        internal bool Enabled {
            get { return enabled; }
            set 
            { 
                enabled = value;
                if (enabled == false)
                    TintColor = Color.DarkGray;
                else
                    TintColor = Color.White;
            }

        }

        protected SpriteBatch Batch { get; set; }   ///< Reference to the SpriteBatch used to Draw the Sprite object
        protected Texture2D Texture { get; set; }   ///< Reference to the Texture used to render to Sprite Object
        protected Vector2 Position { get; set; }    ///< Reference to the Sprite Position in 2D space.
        protected Color TintColor { get; set; }     ///< TintColor Setting [Property]

        #endregion

        #region Constructors

        /// \brief Constructor (no texture)
        /// \note This constructor sets an initial 2D Position (Vector2:x,y)
        /// \note The constructor also sets the default TintColor to Color.White (uses Alpha:255)
        /// \author Steven Duda
        protected Sprite(SpriteBatch batch, Vector2 initialPosition)
        {
            Batch = batch;
            Position = initialPosition;
            TintColor = Color.White;
            Enabled = true;
        }

        // Null Constuctor (for singleton instancing)
        protected Sprite() 
        {
            Enabled = false;
        }

        #endregion

        #region Public Methods

        /// \brief Draws the Sprite using texture, position, and color
        /// This Method should be overriden for an overloaded / alternative Draw
        /// \author Steven Duda
        internal virtual void Draw()
        {
            Batch.Draw(Texture, Position, TintColor);
        }

        /// \brief Update the sprite. Base Method does nothing
        /// \author Steven Duda
        internal abstract void Update();

        /// \brief Checks if 2D Coordinates(X,Y) are within the Sprite
        /// \author Steven Duda
        /// \param x any X position coordinate
        /// \param y any Y position coordinate
        /// \return  Target within the Sprite? (true/false)
        /// \note This can be used to Check if the Mouse is Over the sprite
        internal bool isInSprite(int x, int y)
        {
            return ((x >= Position.X) &&
                    (x <= (Position.X + Texture.Width)) &&
                    (y >= Position.Y) &&
                    (y <= (Position.Y + Texture.Height)));
        }

        /// \brief Implement IDisposable
        /// Clean up unmanaged resources via Dispose Method.
        /// Remove object from Garbage Collection Finalization Queue
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// \brief overridable Dispose Method
        /// Use this to perform any clean up of any resources
        /// Both Managed and Unmanaged Resources can by Disposed
        /// Base Class Does not Implement this function.
        /// \author Steven Duda
        internal virtual void Dispose(bool disposing)
        {
        }

        #endregion
    }
}
